﻿using System;
using System.IO;
using System.Windows.Forms;
using DevExpress.XtraNavBar;
using Newtonsoft.Json;
using EyFramework.Common;
using EyFramework.Common.Utils;
using EyFramework.AutoCode.DbFunc;
using EyFramework.AutoCode.Entity;
using EyFramework.Library;

namespace EyFramework.AutoCode
{
    public partial class FrmDBSetting : FrmDevBase
    {
        private IDbPanel _dbPanel;

        public FrmDBSetting()
        {
            InitializeComponent();
        }

        private void FrmDBSetting_Load(object sender, EventArgs e)
        {
            #region 初始化Oracle的数据库配置

            PublicGlobal.DataBases.OracleDbs.ForEach(p =>
            {
                var item = new NavBarItem { Caption = p.ConnectName, Tag = p };
                var itemLink = new NavBarItemLink(item);
                item.LinkClicked += item_LinkClicked;
                nbgOracle.ItemLinks.Add(itemLink);
                nbcAdd.Items.Add(item);
            });
            nbgOracle.ItemLinks.Add(new NavBarSeparatorItem());
            var itemAdd = new NavBarItem { Caption = "新增Oracle" };
            var itemLinkAdd = new NavBarItemLink(itemAdd);
            itemAdd.LinkClicked += itemAdd_LinkClicked;
            nbgOracle.ItemLinks.Add(itemLinkAdd);
            nbcAdd.Items.Add(itemAdd);

            #endregion 初始化Oracle的数据库配置

            #region 初始化Sql的数据库配置\

            if (PublicGlobal.DataBases.SqlserverDbs != null)
            {
                PublicGlobal.DataBases.SqlserverDbs.ForEach(p =>
                {
                    var item = new NavBarItem { Caption = p.ConnectName, Tag = p };
                    var itemLink = new NavBarItemLink(item);
                    item.LinkClicked += Item_LinkClicked; ;
                    nbgSql.ItemLinks.Add(itemLink);
                    nbcAdd.Items.Add(item);
                });
            }

            nbgSql.ItemLinks.Add(new NavBarSeparatorItem());
            var itemAddSql = new NavBarItem { Caption = "新增Sqlserver" };
            var itemLinkAddSql = new NavBarItemLink(itemAddSql);
            itemAddSql.LinkClicked += ItemAddSql_LinkClicked;
            nbgSql.ItemLinks.Add(itemLinkAddSql);
            nbcAdd.Items.Add(itemAdd);

            #endregion 初始化Sql的数据库配置\
        }

        private void Item_LinkClicked(object sender, NavBarLinkEventArgs e)
        {
            _dbPanel = new SqlServerPanel(e.Link.Item.Tag as SqlserverDb);
            SqlServerPanel sdp = (SqlServerPanel)_dbPanel;
            sdp.Dock = DockStyle.Fill;
            sdp.AutoScroll = true;
            this.pcMain.Controls.Clear();
            this.pcMain.Controls.Add(sdp);
        }

        private void ItemAddSql_LinkClicked(object sender, NavBarLinkEventArgs e)
        {
            _dbPanel = new SqlServerPanel();
            SqlServerPanel sdp = (SqlServerPanel)_dbPanel;
            sdp.Dock = DockStyle.Fill;
            sdp.AutoScroll = true;
            this.pcMain.Controls.Clear();
            this.pcMain.Controls.Add(sdp);
        }

        private void item_LinkClicked(object sender, NavBarLinkEventArgs e)
        {
            _dbPanel = new DbOraclePanel(e.Link.Item.Tag as OracleDb);
            DbOraclePanel uc = (DbOraclePanel)_dbPanel;
            uc.Dock = DockStyle.Fill;
            uc.AutoScroll = true;
            this.pcMain.Controls.Clear();
            this.pcMain.Controls.Add(uc);
        }

        private void itemAdd_LinkClicked(object sender, NavBarLinkEventArgs e)
        {
            _dbPanel = new DbOraclePanel();
            DbOraclePanel uc = (DbOraclePanel)_dbPanel;
            uc.Dock = DockStyle.Fill;
            uc.AutoScroll = true;
            this.pcMain.Controls.Clear();
            this.pcMain.Controls.Add(uc);
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (_dbPanel.SaveDbConnect())
            {
                if (FileUtil.FileIsExist(PublicGlobal.dbSettingFile))
                {
                    FileUtil.DeleteFile(PublicGlobal.dbSettingFile);
                }
                FileUtil.WriteText(PublicGlobal.dbSettingFile, JsonConvert.SerializeObject(PublicGlobal.DataBases));
                Globals.ShowTips("保存成功!");
            }
        }

        private void btnTestConnect_Click(object sender, EventArgs e)
        {
            if (_dbPanel != null)
            {
                if (_dbPanel.TestConnect())
                {
                    Globals.ShowTips("(✧◡✧)连接成功！");
                }
            }
        }

        private void simpleButton1_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}